package com.maisonliberte.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.maisonliberte.conf.Config;

public class RateUtil {

	private static Map<String, Float> currentRate = new HashMap<String, Float>();
	
	public static float getJapanRate(){
		
		if(currentRate.containsKey(DateUtil.getDateNowYYYYMMDD())){
			return currentRate.get(DateUtil.getDateNowYYYYMMDD());
		}
		
		String exchangeInfo = "";
		WebDriver wd = new HtmlUnitDriver();
		wd.get("http://blog.naver.com/a1030ys/130155701179");
		WebElement el = wd.findElement(By.linkText("http://www.naver.com/include/timesquare/widget/exchange.xml"));
		String handlername = wd.getWindowHandle();
		if(el != null){
			el.click();
			for(String str : wd.getWindowHandles()){
				if(!handlername.equals(str)){
					handlername = str;
					break;
				}
			}
			wd.switchTo().window(handlername);
			exchangeInfo = wd.getPageSource();
		}
		
		String rate = "";
		float result = -1;
		
		if(!exchangeInfo.isEmpty()){
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
				DocumentBuilder parser = factory.newDocumentBuilder(); 
				// XML 내용이 담긴 문자열을 읽어들여서 DOM을 구성 
				InputSource is = new InputSource();
				is.setCharacterStream(new StringReader(exchangeInfo));
				Document doc = parser.parse(is);
				
				NodeList nodeList = doc.getElementsByTagName("currency");
				for(int i = 0; i < nodeList.getLength(); i++){
					Node currencyNode = nodeList.item(i);
					if (currencyNode.getNodeType() == Node.ELEMENT_NODE) {
						Element currencyElmnt = (Element) currencyNode;
						NodeList nameList= currencyElmnt.getElementsByTagName("hname");
						Element nameElmnt = (Element) nameList.item(0);
						Node name = nameElmnt.getFirstChild();
						String nameNodeValue = name.getNodeValue();
						if(nameNodeValue.equals("일본")){
							
							System.out.println("hname : " + nameNodeValue);
							
							NodeList buyList= currencyElmnt.getElementsByTagName("buy");
							Element buyElmnt = (Element) buyList.item(0);
							Node buy = buyElmnt.getFirstChild();
							rate = buy.getNodeValue();
							
							//System.out.println("rate : " + rate);
							
							
							break;
							
							
						}else{
							//System.out.println(nameNodeValue);
						}
					}
				}
			
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(!rate.isEmpty()){
			result = Float.parseFloat(rate)*Config.getInstance().plusTesuryo;
		}
		
		currentRate.put(DateUtil.getDateNowYYYYMMDD(), result);
		return currentRate.get(DateUtil.getDateNowYYYYMMDD());
	}

}
